<?php
 class actionlog_model extends CI_Model{
	function actionlog_model(){
		parent::__construct();
		$this->load->helper('url');
	}
	function saveActionLog($action,$obejct_name,$object_id,$content,$email,$parent_id){
		$insert_data = array("action"=>$action,"object_name"=>$obejct_name,"content"=>$content,"email"=>$email,"parent_id"=>$parent_id);
		if(isset($object_id)&&!empty($object_id)){
			$insert_data["object_id"]=$object_id;
		}
		$this->db->insert('adminlogactions', $insert_data);
		return $this->db->insert_id();
	}
	
	function searchUserLog($searchKey, $actionType, $fromDate, $toDate, $email, $startIndex,$endIndex){
		//order by id desc limit 10
		$strSQL = " select al.* from adminlogactions al where true  ";
		if($fromDate!=0){
			$strSQL.= " and CAST(al.created_date as date) >= '".$fromDate."'";
		}
		if($toDate!=0){
			$strSQL.=" and CAST(al.created_date as date) <= '".$toDate."' ";			
		}
		if($actionType!="All"){
			$strSQL.=" and object_name = \"".$actionType."\"";
		}
		if($email!=""){
			$strSQL.=" and al.email like '%".$email."%'";
		}		
		
		if($searchKey != ""){
			$strSQL.= " and al.content like '%".$searchKey."%'";
		}
		
		$strSQL.=" order by al.id desc  ";		
		if($endIndex>0){
		$strSQL .= " limit ".$startIndex.",".($endIndex-$startIndex);
		}
		
		$result = $this->db->query($strSQL);
		return $result->result();
	}
	
	function searchUserLogTotal($searchKey, $actionType, $fromDate, $toDate, $email){
		//order by id desc limit 10
		$strSQL = " select al.* from adminlogactions al where true  ";
		if($fromDate!=0){
			$strSQL.= " and CAST(al.created_date as date) >= '".$fromDate."'";
		}
		if($toDate!=0){
			$strSQL.=" and CAST(al.created_date as date) <=  '".$toDate."'";			
		}
		if($actionType!="All"){
			$strSQL.=" and object_name =  \"".$actionType."\"";
		}
		if($email!=""){
			$strSQL.=" and al.email like '%".$email."%'";
		}		
		
		if($searchKey != ""){
			$strSQL.= " and al.content like '%".$searchKey."%'";
		}
		
		$strSQL.=" order by al.id desc  ";				
		$result = $this->db->query($strSQL);
		
		return $result->result();
	}
	
	function getActionType(){
		$this->db->select('object_name');
		$this->db->distinct();
		$this->db->order_by('object_name asc');
		$result = $this->db->get('adminlogactions');
		$arr = array("All"=>"All", 
					"Maintain order"=>"Maintain Order",
					"Maintain product"=>"Maintain Product",
					"Maintain user"=>"Maintain User",
					"Maintain category"=>"Maintain Category",
					"Maintain shipping setting"=>"Maintain Shipping Setting",
					"Maintain flatfee setting"=>"Maintain Flat Fee Setting",
					"Accept givemart's policy"=>"Accept Givemart's Policy",
					"Update Systemparamter"=>"Update System Paramter",
					"Update emailtemplate"=>"Update Email Template",
					"Manage content page"=>" Manage Content Page",
					"Maintain non-profit organization (charity)"=>"Maintain Non-Profit Organization (Charity)",
					"Maintain non-profit organization (church)"=>"Maintain Non-Profit Organization (Church)",
					"Maintain non-profit organization (school)"=>"Maintain Non-Profit Organization (School)",
					"Maintain organization logo"=>"Maintain Organization Logo",
					"Maintain discount"=>"Maintain Discount",
					"Maintain option type"=>"Maintain Option Type"
					);
		//return $result->result();
		return $arr;
	}
	
	function delete_logs($listIds)
	{		
		$this->load->database();
		$strSQL="delete from adminlogactions where id in (".$listIds.")";
		//echo($strSQL);
		$result=$this->db->query($strSQL);
		return $result;	
	}
 }
 ?>